package com.lunhui.service.lunhui.service.business.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lunhui.service.lunhui.service.business.UserService;
import com.lunhui.user.client.RoseClient;
import com.lunhui.user.client.UserClient;
import com.lunhui.user.client.model.RoseDto;
import com.lunhui.user.client.model.UserDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author renminghe-kg
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Reference
    private UserClient userClient;

    @Reference
    private RoseClient roseClient;

    @Override
    public Integer saveToDB(String account, String password) throws RuntimeException {

        Integer resFlag;

        try {
            resFlag =  userClient.saveToDB(account, password);
        } catch (Exception e) {
            logger.error("调用user服务存储账户接口失败: " + e.getMessage());
            throw new RuntimeException("调用user服务存储账户接口失败: " + e.getMessage());
        }

        if (resFlag != 1) {
            return -1;
        }

        return resFlag;

    }

    @Override
    public UserDto findByAccountAndPassword(String account, String password) throws RuntimeException {
        try {
            return userClient.findByAccountAndPassword(account, password);
        } catch (Exception e) {
            logger.error("调用user服务查询账户接口失败: " + e.getMessage());
            throw new RuntimeException("调用user服务查询账户接口失败: " + e.getMessage());
        }
    }

    @Override
    public List<RoseDto> findRoseNameListByUserId(Integer userId) throws RuntimeException {
        try {
            return roseClient.findRoseIdAndNameListByUserId(userId);
        } catch (Exception e) {
            logger.error("调用user服务查询角色名列表接口失败: " + e.getMessage());
            throw new RuntimeException("调用user服务查询角色名列表接口失败: " + e.getMessage());
        }
    }

    @Override
    public Integer updateUserInfo(UserDto userDto) throws RuntimeException {
        return userClient.updateUserInfo(userDto);
    }

    @Override
    public Integer crateRose(RoseDto roseDto) {
        try {
            return roseClient.saveToDB(roseDto);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public RoseDto findRoseByUserIdAndRoseName(Integer userId, String roseName) {
       return roseClient.findRoseByUserIdAndRoseName(userId, roseName);
    }

    @Override
    public RoseDto findRoseByRoseId(Integer roseId) {
        return roseClient.findRoseByRoseId(roseId);
    }
}